import utils from './../utils'
import * as types from '../constants/cart'
import store from 'store'


//获取买家购物车列表
export const getCartList =(data,config={})=>{
	data= {...data,...{func:'cart-list'}}
    return (dispatch,getState)=>{
		return utils.form(utils.url.index,data)
		.then(res=>{
			if(res.code == 200){
				let json=res.data 
				localStorage.setItem('cart', JSON.stringify(json)||[]);
				return dispatch({type:types.GET_CART_LIST,data:json||[]})
			}
			
		})
	}
}

//保存商品信息
export const addCart =(data) =>{
	return (dispatch,getState)=>{
		const options = getState().addCartInfo.toJS()
		//如果主商品的spec 改变了，附属商品清空
		if(options.goods_id && options.goods_id == data.goods_id && options.goods_spec_id != data.goods_spec_id ){
			options.gift_goods={}
			options.exchange_goods={}
			data ={...options,...data}
		}else{
			data ={...options,...data}
		}

		let addData={func:'cart-add'}
		//若有主商品情况下，需要保存到数据库
		if(data.goods_id){
			let _data= Object.assign({},data)
			_data.gift_goods=_data.gift_goods ? JSON.stringify(_data.gift_goods):''
			_data.exchange_goods= _data.exchange_goods ? JSON.stringify(_data.exchange_goods) : ''
			return utils.form(utils.url.index,{..._data,...addData})
			.then(res=>{
				return dispatch({type:types.ADD_CART_INFO,data:data})
			})
		}else{
			return dispatch({type:types.ADD_CART_INFO,data})
		}
		
	}
}

//更新购物车
export const updateCartList =(data,config={})=>{
    return (dispatch,getState)=>{
    	localStorage.setItem('cart', JSON.stringify(data));
		return dispatch({type:types.GET_CART_LIST,data:data})
	}
}

//更改购物车中主商品或兑换商品的数量  加一或减一
export const changeNum =(data,config={})=>{
	data= {...data,...{func:'change-num'}}
    return (dispatch,getState)=>{
		return utils.form(utils.url.index,data)
		.then(res=>{
			config.callback && config.callback(res)
		})
	}
}
//直接修改购物车数量
export const changeCartNum =(data,config={})=>{
	data= {...data,...{func:'set-cart-num'}}
    return (dispatch,getState)=>{
		return utils.form(utils.url.index,data)
		.then(res=>{
			config.callback && config.callback(res)
		})
	}
}
export const setCartStatus =(data,config={})=>{
	data= {...data,...{func:'set-cart-status'}}
    return (dispatch,getState)=>{
		return utils.form(utils.url.index,data)
		.then(res=>{
			config.callback && config.callback(res)
		})
	}
}

//删除购物车中的兑换品
export const delExchange =(data,config={})=>{
	data= {...data,...{func:'cart-del-exchange'}}
    return (dispatch,getState)=>{
    	let options = getState().cartList.toJS()
		return utils.form(utils.url.index,data)
		.then(res=>{
			let cart=options.find(item => item.cart_id == data.cart_id);
			cart = cart.gift_list.filter(item => item.cart_id != data.exchange_cart_id);
			console.log(options)
			localStorage.setItem('cart', JSON.stringify(options));
			return dispatch({type:types.GET_CART_LIST,data:options})
		})
	}
}

//单条 删除购物车数据
export const delOneCart =(data,config={})=>{
	data= {...data,...{func:'cart-del'}}
    return (dispatch,getState)=>{
    	let options = getState().cartList.toJS()
		return utils.form(utils.url.index,data)
		.then(res=>{
			options=options.filter(item => item.cart_id != data.cart_id);
			localStorage.setItem('cart', JSON.stringify(options));
			return dispatch({type:types.GET_CART_LIST,data:options})
		})
	}
}

//多条  删除购物车数据. 
export const delCarts =(data,config={})=>{
	data= {...data,...{func:'cart-del'}}
    return (dispatch,getState)=>{
    	let options = getState().cartList.toJS()
		return utils.form(utils.url.index,data)
		.then(res=>{
			localStorage.setItem('cart', []);
			return dispatch({type:types.GET_CART_LIST,data:[]})
		})
	}
}

//获取已勾选的购物车商品
export const getCartListbyStore =()=>{
    return (dispatch,getState)=>{
    	let options = getState().cartList.toJS()
    	if(options.length==0)
    		options=localStorage.getItem('cart')?JSON.parse(localStorage.getItem('cart')):[]
		return dispatch({type:types.GET_CART_LIST,data:options})
	}
}
//获取立即购买的商品
export const getTempCartbyStore =()=>{
    return (dispatch,getState)=>{
    	let options = getState().tempCart.toJS()
    	if(options.length==0)
    		options=localStorage.getItem('tempCart')?JSON.parse(localStorage.getItem('tempCart')):[]
		return dispatch({type:types.SET_TEMP_CART,data:options})
	}
}

//立即购买的商品
export const addTempCart =(data) =>{
	return (dispatch,getState)=>{
		localStorage.setItem('tempCart', JSON.stringify(data)||[]);
		return dispatch({type:types.SET_TEMP_CART,data:data||[]})
	}
}


